package com.concerneyes.mine.TabActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.health.HealthActivity;
import com.concerneyes.health.HealthAddUserActivity;
import com.concerneyes.mine.Task.GlassManImpl;
import com.concerneyes.mine.Task.GlassManListener;
import com.concerneyes.mine.adapter.MyUserControlAdapter;
import com.concerneyes.mine.model.MyGlassManModel;

import java.util.List;

import common.AbsActivity;
import util.ActivityUtil;
import view.MyUniversalDialog;

/**
 * 用户管理
 */

public class MyUserControl extends AbsActivity implements GlassManListener,
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, MyUserControlAdapter.OnEditClick {
    private ListView lv_com;
    private MyUserControlAdapter adapter;
    private GlassManImpl impl;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.listview);
        String title = getIntent().getExtras().getString("title");
        setTitle(title);
        getRightText().setVisibility(View.VISIBLE);
        getRightText().setText(R.string.add);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        impl.listGlassMan();
    }

    @Override
    public void onRightTextAction(View v) {
        super.onRightTextAction(v);
        Bundle bundle = new Bundle();
        bundle.putBoolean("isAdd", true);
        ActivityUtil.setIntent(this, HealthAddUserActivity.class, bundle);
    }

    private void initView() {
        impl = new GlassManImpl(this, this);
        impl.listGlassMan();
        lv_com = (ListView) findViewById(R.id.lv_com);
        adapter = new MyUserControlAdapter(this, this);
        lv_com.setOnItemClickListener(this);
        lv_com.setOnItemLongClickListener(this);
    }

    @Override
    public void glassManListSuccess(List<MyGlassManModel> model) {
        adapter.clear();
        adapter.addDatas(model);
        lv_com.setAdapter(adapter);
    }

    @Override
    public void glassManListFail() {

    }

    @Override
    public void deleteGlassManSuccess(List<MyGlassManModel> model) {
        impl.listGlassMan();
    }

    @Override
    public void deleteGlassManFail() {

    }

    @Override
    public void addGlassManSuccess() {

    }

    @Override
    public void addGlassManFail() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("isUser",true);
        bundle.putString("manId",String.valueOf(adapter.getItem(position).getId()));
        ActivityUtil.setIntent(this, HealthActivity.class, bundle);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        ChangeAdressDialog(position);
        return true;
    }

    private void ChangeAdressDialog(final int position) {
        final MyUniversalDialog dialog = new MyUniversalDialog(this);
        int width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_adress, null), MyUniversalDialog.DialogGravity.CENTER, -2, 5 * width / 6);
        dialog.setCanceledOnTouchOutside(true);
        TextView change = (TextView) dialog.findViewById(R.id.change);
        TextView set_mr = (TextView) dialog.findViewById(R.id.set_mr);
        TextView detele = (TextView) dialog.findViewById(R.id.detele);
        change.setVisibility(View.GONE);
        set_mr.setVisibility(View.GONE);
        final int userId = adapter.getItem(position).getId();
        detele.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                impl.deteleGlassMan(userId);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void editClick(int position) {
        int userId = adapter.getItem(position).getId();
        Bundle bundle = new Bundle();
        bundle.putBoolean("isAdd", false);
        bundle.putInt("glassMan_position", position);
        ActivityUtil.setIntent(this, HealthAddUserActivity.class, bundle);
    }
}
